package com.demo.resourcecode.c11mvc;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo.resourcecode.c9mybatis.User;

//@RestController("/message.do")
@RestController
public class UserController {

	public UserController(){
		System.out.println("init");
	}

	@GetMapping("/user")
	public String user(){
		return "test";
	}

	@PutMapping("/put")
	public String put(@RequestParam("id") int id){
		return "test" + id;
	}

	@GetMapping("/test/{id}")
	public String test1(@PathVariable("id") int id){
		String s = null;
		//s.indexOf(1);
		//throw new MyException(); // ResponseStatusExceptionResolver用于处理异常类上包含@ResponseStatus注解的异常信息
		return "test" + id;
	}

	@GetMapping("/test")
	public String test2(){
		String s = null;
		//s.indexOf(1);
		return "test";
	}

	@RequestMapping(path = "/test2/{id}", method = RequestMethod.GET, consumes="application/json", produces="application/json", headers = "code=100", params = "id=123")
	public String test2(@PathVariable("id") int id){
		String s = null;
		//s.indexOf(1);
		//throw new MyException(); // ResponseStatusExceptionResolver用于处理异常类上包含@ResponseStatus注解的异常信息
		return "test2" + id;
	}

	@GetMapping("/test3")
	public String test3(@RequestParam("urlId") int urlId, HttpServletRequest request){
		return "test" + urlId;
	}

	@PostMapping("/test4")
	public String test4(@RequestBody User user){
		return "test" + user;
	}

	@PostMapping("/test5")
	public String test5(User user){
		return "test" + user;
	}

	@GetMapping("/user2")
	public User user2(){
		User user = new User();
		user.setId(1);
		user.setName("aaa");
		return user;
	}

}
